package light.head.config;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.framework.entity.Handle;
import light.head.framework.entity.Menu;
import light.head.framework.entity.Module;
import light.head.framework.entity.SystemInfo;
import light.head.framework.utils.XmlUtils;

import org.dom4j.Document;
import org.dom4j.Element;

public class HandleParm {
	
	private final static String path = "/config/handle.xml";
	
	@SuppressWarnings("unchecked")
	public static boolean loadAll() {
		Document xml = loadHandleXml();
		Element element = xml.getRootElement();
		
		
		if (null != element && element.elements().size() > 0) {
			List<SystemInfo> list = new ArrayList<SystemInfo>();
			SystemInfo tms = null;
			Module tmo = null;
			Menu tmu = null;
			Handle th = null;
			
			Map<String, Boolean> handleMap = new HashMap<String, Boolean>();
			for (Iterator<Element> systemInfoList = element.elementIterator(); systemInfoList.hasNext();) {
				Element systemInfo = systemInfoList.next();
				
				if (null != systemInfo) {
					tms = new SystemInfo();
					
					tms.setCode(systemInfo.attributeValue(Parm.CODE));
					tms.setName(systemInfo.attributeValue(Parm.NAME));
					tms.setIcon(systemInfo.attributeValue(Parm.ICON));
					
					List<Module> tmoList = new ArrayList<Module>();
					for (Iterator<Element> moduleList = systemInfo.elementIterator(); moduleList.hasNext();) {
						Element module = moduleList.next();
						
						if (null != module) {
							tmo = new Module();
							tmo.setCode(module.attributeValue(Parm.CODE));
							tmo.setName(module.attributeValue(Parm.NAME));
							tmo.setIcon(module.attributeValue(Parm.ICON));
							
							List<Menu> tmList = new ArrayList<Menu>();
							for (Iterator<Element> menuList = module.elementIterator(); menuList.hasNext();) {
								Element menu = menuList.next();
								
								if (null != menu) {
									tmu = new Menu();
									tmu.setCode(menu.attributeValue(Parm.CODE));
									tmu.setName(menu.attributeValue(Parm.NAME));
									tmu.setIcon(menu.attributeValue(Parm.ICON));
									tmu.setDes(menu.attributeValue(Parm.DES));
									tmu.setShortcut(null != menu.attributeValue(Parm.SHORTCUT) && menu.attributeValue(Parm.SHORTCUT).equals(Sys.S_ONE) ? true : false);
									tmu.setMs(null != menu.attributeValue(Parm.MS) && menu.attributeValue(Parm.MS).equals(Sys.S_ONE) ? true : false);
									
									List<Handle> hList = new ArrayList<Handle>();
									for (Iterator<Element> handleList = menu.elementIterator(); handleList.hasNext();) {
										Element handle = handleList.next();
										
										if (null != handle) {
											th = new Handle();
											th.setCode(handle.attributeValue(Parm.CODE));
											th.setName(handle.attributeValue(Parm.NAME));
											th.setHide(Boolean.parseBoolean(handle.attributeValue(Parm.HIDE)));
											th.setLog(Boolean.parseBoolean(handle.attributeValue(Parm.LOG)));
											
											hList.add(th);
											
											// 菜单的code是每个Controller的基本操作链接，因此也需要加入到handleMap中，handleMap将所有需要验证的操作信息都缓存起来
											handleMap.put(th.getCode(), th.getLog());
										}
									}
									
									tmu.setHandleList(hList);
									tmList.add(tmu);
									
									// 菜单的code是每个Controller的基本操作链接，因此也需要加入到handleMap中，handleMap将所有需要验证的操作信息都缓存起来
									handleMap.put(tmu.getCode(), false);
								}
							}
							tmo.setMenuList(tmList);
							tmoList.add(tmo);
						}
					}
					tms.setModuleList(tmoList);
				}
				list.add(tms);
			}
			
			
			
//			Cache.moduleList = list;
			Cache.systemInfoList = list;
			Cache.handleMap = handleMap;
			return true;
			
		}
		return false;
	}
	
	public static Document loadHandleXml() {
		InputStream in = Config.class.getResourceAsStream(path);
		Document xml = XmlUtils.loadXML(in);
		return xml;
	}
}
